package com.qk.controller;

import com.qk.Dto.UserDto;
import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.User;
import com.qk.server.UserServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserServer userServer;
    //查询列表语句
    @GetMapping
    public Result findUserByList(UserDto userDto){
            PageResult result =userServer.findUserByList(userDto);
        return Result.success(result);
    }
    //添加
    @Log
    @PostMapping
    public Result addUser(@RequestBody User user){
        userServer.addUser(user);
        return Result.success();
    }
    //删除
    @Log
    @DeleteMapping("/{ids}")
    public Result deleteUserByIds(@PathVariable List<Integer> ids){
        userServer.deleteUserByIds(ids);
        return Result.success();
    }
    //回显
    @GetMapping("/{id}")
    public Result findUserById(@PathVariable Integer id){
        User user = userServer.findUserById(id);
        return Result.success(user);
    }
    //修改
    @Log
    @PutMapping
    public Result updateUser(@RequestBody User user){
        userServer.updateUser(user);
        return Result.success();
    }
    //根据角色表示查询用户
    @GetMapping("/role/{roleLabel}")
    public Result selectUserByRole(@PathVariable String roleLabel){
        List<User> user = userServer.findUserByRole(roleLabel);
        return Result.success(user);
    }
    //查询所有
    @GetMapping("/list")
    public Result findUserAll(){
        userServer.findUserAll();
        return Result.success();
    }
    //根据部门查询用户
    @GetMapping("/dept/{deptId}")
    public Result findUserByDept(@PathVariable Integer deptId){
        List<User> user = userServer.findUserByDept(deptId);
        return Result.success(user);
    }
}
